package admin.controllers;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.OrderModel;

import search.OrderSearchData;

@SuppressWarnings("serial")
public class SaleProfitServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(SaleProfitServlet.class
			.getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		process(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		process(request, response);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		try {
			boolean toFile = false;
			String action = request.getParameter("action");
			if (action != null && action.equals("toFile")) {
				toFile = true;
			}
			String strFromDate = request.getParameter("fromDate");
			String strToDate = request.getParameter("toDate");
			Date fromDate;
			Date toDate;
			DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
			try {
				fromDate = formatter.parse(strFromDate);
				request.setAttribute("fromDate", strFromDate);
			} catch (Exception e) {
				fromDate = null;
			}
			try {
				toDate = formatter.parse(strToDate);
				request.setAttribute("toDate", strToDate);
			} catch (Exception e) {
				toDate = null;
			}
			OrderSearchData orderSearch = new OrderSearchData();
			orderSearch.searchByDate(fromDate, toDate);
			List<OrderModel> listResult = orderSearch.getListResult();
			int totalGroupDeal = 0;
			int totalDealQuantity = 0;
			int totalOrderDelivery = 0;
			int totalOrderCheckOut = 0;
			int totalOrderMoney = 0;
			int totalRealMoney = 0;
			TreeMap uniqueOrderId = new TreeMap();
			for (OrderModel orderModel : listResult) {
				uniqueOrderId.put(orderModel.getOrderId(),
						orderModel.getDealId());
				totalDealQuantity += orderModel.getQuantity();
				totalOrderMoney += orderModel.getTotalPrice();
				if (orderModel.getStatus() == 4) {
					totalOrderDelivery += orderModel.getQuantity();
					totalOrderCheckOut += orderModel.getQuantity();
					totalRealMoney += orderModel.getTotalPrice();
				}
			}
			totalGroupDeal = uniqueOrderId.size();
			request.setAttribute("totalGroupDeal", totalGroupDeal);
			request.setAttribute("totalDealQuantity", totalDealQuantity);
			request.setAttribute("totalOrderDelivery", totalOrderDelivery);
			request.setAttribute("totalOrderCheckOut", totalOrderCheckOut);
			request.setAttribute("totalOrderMoney", totalOrderMoney);
			request.setAttribute("totalRealMoney", totalRealMoney);
			if (toFile) {
				request.getRequestDispatcher(
						"/admin/pages/file_sale_profit.jsp").forward(request,
						response);
			} else {
				request.getRequestDispatcher("/admin/pages/sale_profit.jsp")
						.forward(request, response);
			}
		} catch (ServletException e) {
			System.out.println("Error when try to get sale profit !");
			log.warning("Error when try to get sale profit !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}
}
